@page "/loveTime"

<h1>爱情计时器</h1>

<div class="timer">我们相识已经过去了：@elapsedTimes["meet"].ElapsedTime</div>
<div class="timer">我们相爱已经过去了：@elapsedTimes["love"].ElapsedTime</div>
<div class="timer">我们步入婚姻已经过去了：@elapsedTimes["married"].ElapsedTime</div>

<h2>输入你的时间节点和想要计算的类型</h2>


@using System.Globalization
@inject IJSRuntime JSRuntime

<h3>爱情计时器</h3>

<div class="date-bar">
    <!-- 年份 -->
    <FluentSelect TOption="string"
                  Label="年份："
                  @bind-Value="selectedYear"
                  class="custom-select"
                  Width="120px">
        @foreach (var y in years)
        {
            <FluentOption Value="@y">@y</FluentOption>
        }
    </FluentSelect>

    <!-- 月份 -->
    <FluentSelect TOption="string"
                  Label="月份："
                  @bind-Value="selectedMonth"
                  class="custom-select"
                  Width="100px">
        @foreach (var m in months)
        {
            <FluentOption Value="@m">@m</FluentOption>
        }
    </FluentSelect>

    <!-- 日期 -->
    <FluentSelect TOption="string"
                  Label="日期："
                  @bind-Value="selectedDay"
                  class="custom-select"
                  Width="100px">
        @foreach (var d in days)
        {
            <FluentOption Value="@d">@d</FluentOption>
        }
    </FluentSelect>
</div>


<FluentStack  Horizontal="true" HorizontalGap="100" Style="justify-content:center">
	<FluentButton class="timer" OnClick="@(() => CalculateElapsedTime("meet"))">计算相识时间</FluentButton>
	<FluentButton class="timer" OnClick="@(() => CalculateElapsedTime("love"))">计算恋爱时间</FluentButton>
	<FluentButton class="timer" OnClick="@(() => CalculateElapsedTime("married"))">计算结婚时间</FluentButton>
</FluentStack >

@code {
	private class ElapsedTimeInfo
	{
		public string ElapsedTime { get; set; } = string.Empty;
		public string ElapsedTimeDay { get; set; } = string.Empty;
		public string ElapsedTimeMonths { get; set; } = string.Empty;
		public string ElapsedTimeYear { get; set; } = string.Empty;
	}

	private Dictionary<string, ElapsedTimeInfo> elapsedTimes = new()
	{
		{ "meet", new ElapsedTimeInfo() },
		{ "love", new ElapsedTimeInfo() },
		{ "married", new ElapsedTimeInfo() }
	};

	private List<string> years = Enumerable.Range(1950, DateTime.Now.Year - 1949).Select(y => y.ToString()).ToList();
	private List<string> months = Enumerable.Range(1, 12).Select(m => m.ToString()).ToList();
	private List<string> days = Enumerable.Range(1, 31).Select(d => d.ToString()).ToList();

	private string selectedYear = "1950";
	private string selectedMonth = "1";
	private string selectedDay = "1";

	private readonly Dictionary<string, DateTime> timePoints = new()
	{
		{ "meet", new DateTime(2018, 4, 22) },
		{ "love", new DateTime(2022, 2, 25) },
		{ "married", new DateTime(2023, 8, 8) }
	};

	protected override void OnInitialized()
	{
		foreach (var timePoint in timePoints)
		{
			CalculateElapsedTime(timePoint.Value, timePoint.Key);
		}
	}

	private void CalculateElapsedTime(DateTime pastDate, string type)
	{
		var elapsed = DateTime.Now - pastDate;
		string formattedElapsedTime = $"{elapsed.Days} 天, {elapsed.Hours} 小时, {elapsed.Minutes} 分钟, {elapsed.Seconds} 秒";

		var elapsedTimeInfo = elapsedTimes[type];
		elapsedTimeInfo.ElapsedTime = formattedElapsedTime;

		int totalYears = elapsed.Days / 365;
		int totalMonths = (elapsed.Days % 365) / 30;
		int totalDays = (elapsed.Days % 365) % 30;

		elapsedTimeInfo.ElapsedTimeYear = totalYears.ToString();
		elapsedTimeInfo.ElapsedTimeMonths = totalMonths.ToString();
		elapsedTimeInfo.ElapsedTimeDay = totalDays.ToString();
	}

	private void CalculateElapsedTime(string type)
	{
		if (int.TryParse(selectedYear, out int year) &&
			int.TryParse(selectedMonth, out int month) &&
			int.TryParse(selectedDay, out int day))
		{
			DateTime selectedDate = new DateTime(year, month, day);
			var elapsed = DateTime.Now - selectedDate;

			int totalYears = elapsed.Days / 365;
			int totalMonths = (elapsed.Days % 365) / 30;
			int totalDays = (elapsed.Days % 365) % 30;

			string formattedElapsedTime = $"{elapsed.Days} 天, {elapsed.Hours} 小时, {elapsed.Minutes} 分钟, {elapsed.Seconds} 秒";

			var elapsedTimeInfo = elapsedTimes[type];
			elapsedTimeInfo.ElapsedTime = formattedElapsedTime;
			elapsedTimeInfo.ElapsedTimeYear = totalYears.ToString();
			elapsedTimeInfo.ElapsedTimeMonths = totalMonths.ToString();
			elapsedTimeInfo.ElapsedTimeDay = totalDays.ToString();
		}
	}
}
